package fintecher.com.modules;


import fintecher.com.bean.Upload;
import fintecher.com.service.IAttachmentService;
import fintecher.com.util.SftpUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@RestController
public class UploadController {


    @Autowired
    private SftpUtils sftpUtils;

    @Autowired
    @Qualifier("attachmentService")
    private IAttachmentService attachmentService;
    /**
     * @return
     * @title: ajaxUpload
     * @description: 文件上传
     * @return: AjaxUploadResponse
     */
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public  List<Upload> upload(HttpServletRequest request) {
        String url = "";
        List<Upload> attachmentList = new ArrayList<>();
        String referId = WebUtils.findParameterValue(request, "referId");
        String userid = WebUtils.findParameterValue(request, "userid");
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        synchronized (sftpUtils) {
            try {
                sftpUtils.connect();
                if (multipartResolver.isMultipart(request)) { // 判断request是否有文件上传
                    MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                    Iterator<String> ite = multiRequest.getFileNames();
//                    if (ite.hasNext()) {
                        while (ite.hasNext()) {
                            MultipartFile file = multiRequest.getFile(ite.next());

                          try {
                              String path1 = "import";
                              Date date = new Date();
                              String childFold = DateFormatUtils.format(date, "yyyyMM");
                              path1 = String.format("%s/%s", path1, childFold);
                              sftpUtils.upload(path1, file.getInputStream(),file.getOriginalFilename());
                              url = sftpUtils.getDomain() + path1 +"/"+ file.getOriginalFilename();

                          }catch (Exception e){
                              System.out.println("上传ftp报错");
                              continue;
                          }
                          /* 保存上传信息*/
                            Upload uploadEntity=new Upload();
                            uploadEntity.setUserid(userid);
                            uploadEntity.setUrl(url);
                            uploadEntity.setReferId(referId);
                            Upload attachment = attachmentService.upload(request, file,uploadEntity);
                            attachmentList.add(attachment);
                        }

                        return attachmentList;
//                    }
//                    else {
//                        System.out.println("没有文件可上传");
//                        return null;
//                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != sftpUtils) {
                    sftpUtils.disconnect();
                }
            }

        }

        return null;
    }
}